<!-- /index.php -->
<?php
// 数据库连接
require_once(__DIR__ . '/common/db.php');

// 状态映射
$status_map = [
    1 => '待审核',
    2 => '已通过',
    3 => '已回复',
    4 => '拒绝',
    0 => '已删除'
];

// 姓名脱敏函数
function mask_name($name) {
    $len = mb_strlen($name, 'utf-8');
    if ($len == 2) {
        return mb_substr($name, 0, 1, 'utf-8') . '*';
    } elseif ($len == 3) {
        return mb_substr($name, 0, 1, 'utf-8') . '*' . mb_substr($name, 2, 1, 'utf-8');
    } elseif ($len > 3) {
        return mb_substr($name, 0, 1, 'utf-8') . '*' . mb_substr($name, $len-1, 1, 'utf-8');
    } else {
        return $name;
    }
}

// 处理留言提交
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $id = uniqid('msg');
    $name = trim($_POST['name']);
    $department = trim($_POST['department']);
    $contact = trim($_POST['contact']);
    $content = trim($_POST['content']);
    $ip = $_SERVER['REMOTE_ADDR'];
    if ($name && $content) {
        $stmt = $db->prepare("INSERT INTO messages (id, name, department, contact, ip, content, status) VALUES (:id, :name, :department, :contact, :ip, :content, 1)");
        $stmt->bindValue(':id', $id, SQLITE3_TEXT);
        $stmt->bindValue(':name', $name, SQLITE3_TEXT);
        $stmt->bindValue(':department', $department, SQLITE3_TEXT);
        $stmt->bindValue(':contact', $contact, SQLITE3_TEXT);
        $stmt->bindValue(':ip', $ip, SQLITE3_TEXT);
        $stmt->bindValue(':content', $content, SQLITE3_TEXT);
        $stmt->execute();
        header("Location: index.php?msg=success");
        exit;
    } else {
        header("Location: index.php?msg=empty");
        exit;
    }
}

// 读取提示信息
$msg = '';
if (isset($_GET['msg'])) {
    if ($_GET['msg'] === 'success') {
        $msg = "留言提交成功，等待审核。";
    } elseif ($_GET['msg'] === 'empty') {
        $msg = "姓名和留言内容不能为空！";
    }
}

// 分页参数
$page = max(1, intval($_GET['page'] ?? 1));
$pageSize = 10;
$offset = ($page - 1) * $pageSize;

// 总数、页数
$total = $db->querySingle("SELECT COUNT(*) FROM messages WHERE status IN (2,3)");
$pageCount = max(1, ceil($total / $pageSize));

// 读取分页留言（已通过/已回复）
$res = $db->query("SELECT * FROM messages WHERE status IN (2,3) ORDER BY created_at DESC LIMIT $pageSize OFFSET $offset");
$messages = [];
while ($row = $res->fetchArray(SQLITE3_ASSOC)) {
    $messages[] = $row;
}
?>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <title>工会留言板</title>
    <link rel="icon" href="/assets/logo.ico">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="/assets/style.css?v=<?php echo time(); ?>">
    <!-- 首页专属私有CSS -->
    <style>
body {
    margin: 0;
    font-family: "微软雅黑", sans-serif;
    background: #f5f7fa;
}
.main {
    max-width: 800px;
    margin: 40px auto 0;
    background: #fff;
    border-radius: 14px;
    box-shadow: 0 2px 12px #e0e0e0;
    padding: 38px 32px 32px 32px;
}
.header {
    background: #fff;
    border-bottom: 1px solid #eee;
    padding: 22px 0 18px 0;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 1px 8px #eaeff4;
}
.logo-img {
    height: 44px;
    width: 44px;
    margin-right: 14px;
    object-fit: contain;
}
.header-title {
    font-size: 2em;
    font-weight: 700;
    color: #285bb7;
    letter-spacing: 3px;
}
.msg-title {
    text-align: center;
    font-size: 1.35em;
    margin-bottom: 26px;
    color: #285bb7;
    font-weight: 600;
    letter-spacing: 2px;
}
.msg-list {
    display: flex;
    flex-direction: column;
    gap: 26px;
    margin-bottom: 48px;
}
.msg-card {
    background: #f9fbff;
    border-radius: 12px;
    box-shadow: 0 2px 12px #ecf3ff;
    padding: 24px 22px 18px 22px;
    position: relative;
    border: 1px solid #e5ebf7;
}
.msg-meta {
    display: flex;
    align-items: center;
    gap: 10px;
    color: #789;
    font-size: 0.99em;
    margin-bottom: 9px;
}
.msg-avatar {
    display: inline-block;
    width: 34px;
    height: 34px;
    border-radius: 50%;
    background: #e4eaf5;
    margin-right: 10px;
    font-size: 1.25em;
    text-align: center;
    line-height: 34px;
    color: #3b5cbb;
    font-weight: bold;
    box-shadow: 0 1px 4px #e0e8ff;
}
.msg-meta-detail {
    color: #888;
    font-size: 0.96em;
}
.msg-content {
    font-size: 1.18em;
    color: #273b59;
    margin-bottom: 12px;
    word-break: break-all;
    line-height: 1.65;
}
.msg-reply {
    background: #eaf3fe;
    border-left: 3px solid #4576f5;
    margin-top: 7px;
    padding: 10px 18px;
    border-radius: 6px;
    color: #2562bb;
    font-size: 1.03em;
    font-weight: 500;
    letter-spacing: .2px;
    margin-bottom: 2px;
}
.no-msg {
    color: #888;
    text-align: center;
    margin: 20px 0 24px 0;
    font-size: 1.08em;
}
.pagination {
    margin: 32px 0 22px 0;
    text-align: center;
}
.pagination a, .pagination span {
    display: inline-block;
    min-width: 36px;
    height: 34px;
    line-height: 32px;
    margin: 0 4px;
    border-radius: 8px;
    font-size: 1.08em;
    text-decoration: none;
    color: #285bb7;
    background: #f2f6fd;
    font-weight: 500;
    border: none;
}
.pagination span.current {
    background: #4576f5;
    color: #fff;
    font-weight: bold;
    border: 1.5px solid #285bb7;
}
.pagination a:hover {
    background: #e6eeff;
    color: #134378;
}
/* 我要留言表单区域 */
.form-title {
    font-size: 1.18em;
    color: #285bb7;
    margin-bottom: 12px;
    font-weight: 600;
}
.leave-form-card {
    background: #f6faff;
    border: 1px solid #e6eaf1;
    border-radius: 12px;
    padding: 26px 22px 18px 22px;
    margin-top: 36px;
    box-shadow: 0 1px 7px #e8f0fd;
}
.form-row {
    margin-bottom: 18px;
}
.form-row label {
    display: block;
    color: #444;
    margin-bottom: 4px;
    font-size: 1em;
}
.form-row input,
.form-row textarea {
    width: 100%;
    border: 1px solid #ddd;
    border-radius: 7px;
    padding: 9px;
    font-size: 1em;
    background: #fff;
}
.form-row textarea {
    resize: vertical;
    height: 80px;
}
.form-submit {
    text-align: right;
}
.form-submit button {
    background: #4576f5;
    color: #fff;
    font-size: 1.1em;
    border: none;
    padding: 10px 36px;
    border-radius: 9px;
    cursor: pointer;
    font-weight: 500;
}
.form-submit button:hover {
    background: #244cb1;
}
.tip {
    color: #d94040;
    font-size: 1.08em;
    margin-bottom: 15px;
}
@media (max-width: 900px) {
    .main { max-width: 98vw; padding: 8px 2vw; }
    .msg-card, .leave-form-card { padding: 12px 7vw; }
}
    </style>
</head>
<body>
    <header class="header">
        <img src="/assets/logo.png" alt="logo" class="logo-img">
        <span class="header-title">工会留言板</span>
    </header>

    <div class="main">
        <h2 class="msg-title">留言展示</h2>
        <div class="msg-list">
            <?php if (empty($messages)) { ?>
                <div class="no-msg">暂无留言。</div>
            <?php } else {
                foreach ($messages as $m) { ?>
                    <div class="msg-card">
                        <div class="msg-meta">
                            <span class="msg-avatar"><?php echo mb_substr(mask_name($m['name']),0,1,'utf-8'); ?></span>
                            <span class="msg-meta-detail">
                                <?php echo htmlspecialchars(mask_name($m['name'])); ?>
                                <?php if ($m['department']) echo "（".htmlspecialchars($m['department'])."）"; ?>
                                <?php echo date('Y-m-d H:i', strtotime($m['created_at'])); ?>
                            </span>
                        </div>
                        <div class="msg-content">
                            <?php echo nl2br(htmlspecialchars($m['content'])); ?>
                        </div>
                        <?php if (!empty($m['reply'])) {
                            $admin_reply_name = !empty($m['reply_admin']) ? htmlspecialchars($m['reply_admin']) : '管理员';
                        ?>
                            <div class="msg-reply">
                                <?php echo $admin_reply_name; ?> 回复：<?php echo nl2br(htmlspecialchars($m['reply'])); ?>
                            </div>
                        <?php } ?>
                    </div>
            <?php }
            } ?>
        </div>
        <!-- 分页区 -->
        <div class="pagination">
            <?php if ($page > 1): ?>
                <a href="?page=1">&laquo; 首</a>
                <a href="?page=<?php echo $page - 1; ?>">&lt; 上一页</a>
            <?php endif; ?>
            <?php
            // 分页页码智能显示
            $start = max(1, $page - 2);
            $end = min($pageCount, $page + 2);
            if ($start > 1) echo "<span>...</span>";
            for ($i = $start; $i <= $end; $i++) {
                if ($i == $page) {
                    echo '<span class="current">'.$i.'</span>';
                } else {
                    echo '<a href="?page='.$i.'">'.$i.'</a>';
                }
            }
            if ($end < $pageCount) echo "<span>...</span>";
            ?>
            <?php if ($page < $pageCount): ?>
                <a href="?page=<?php echo $page + 1; ?>">下一页 &gt;</a>
                <a href="?page=<?php echo $pageCount; ?>">末 &raquo;</a>
            <?php endif; ?>
        </div>
        <!-- 我要留言区域 -->
        <div class="leave-form-card">
            <h2 class="form-title">我要留言</h2>
            <?php if ($msg) echo "<div class='tip'>" . htmlspecialchars($msg) . "</div>"; ?>
            <form method="post">
                <div class="form-row">
                    <label>姓名 *</label>
                    <input type="text" name="name" maxlength="15" required>
                </div>
                <div class="form-row">
                    <label>部门</label>
                    <input type="text" name="department" maxlength="32">
                </div>
                <div class="form-row">
                    <label>联系方式</label>
                    <input type="text" name="contact" maxlength="32">
                </div>
                <div class="form-row">
                    <label>留言内容 *</label>
                    <textarea name="content" maxlength="400" required></textarea>
                </div>
                <div class="form-submit">
                    <button type="submit">提交留言</button>
                </div>
            </form>
        </div>
    </div>
    <?php include('./components/footer.php'); ?>
</body>
</html>
